<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="show"
          @click="handleUpdate"
          v-hasPermi="['production:stopReason:edit']"
        >
          分析
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="exportExcel('#test', '异常停机原因维护')"
          v-hasPermi="['production:stopReason:export']"
        >
          导出数据
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="stopReasonList"
      border
      id="test"
      stripe
      highlight-current-row
      :height="tableH"
      @row-click="clickRow"
      :header-cell-style="{ 'text-align': 'center', padding: '0px' }"
    >
      <el-table-column
        label="#"
        fixed="left"
        align="center"
        type="index"
        width="50"
      />
      <el-table-column label="发生日期" width="150" align="center" prop="ztlRq">
        <template #default="scope">
          <span>{{ parseTime(scope.row.ztlRq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="机台编号"
        align="center"
        width="100"
        prop="ztlJtbh"
      />
      <el-table-column
        label="数据类型"
        width="100"
        align="center"
        prop="icName"
      />
      <el-table-column
        label="模具编号"
        width="135"
        align="center"
        prop="moeMjbh"
      />
      <el-table-column
        label="物料代码"
        align="center"
        prop="moeWldm"
        width="135"
      />
      <el-table-column
        label="品名规格"
        show-overflow-tooltip
        prop="itmPmgg"
        width="400"
      />
      <el-table-column
        label="开始时间"
        align="center"
        prop="ztlKssj"
        width="150"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.ztlKssj, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结束时间"
        width="150"
        align="center"
        prop="ztlJssj"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.ztlJssj, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用时" width="100" align="center" prop="ztlGs" />
      <el-table-column
        label="操作人员"
        width="100"
        align="center"
        prop="ztlWknoMc"
      />
      <el-table-column
        label="原因类别"
        width="100"
        align="center"
        prop="ztlLbdm"
      >
        <template #default="scope">
          <dict-tag
            :options="reasonCategoryOptions"
            :value="scope.row.ztlLbdm"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="原因描述"
        align="center"
        prop="ztlYyms"
        width="100"
      />
      <el-table-column
        label="分析人员"
        align="center"
        prop="ztlFxryMc"
        width="100"
      />
      <el-table-column
        label="分析日期"
        align="center"
        prop="ztlFxrq"
        width="150"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.ztlFxrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改采购属性对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="800px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="68px">
        <el-col :span="12">
          <el-form-item label="机台编号" label-width="110px" prop="ztlJtbh">
            <el-input v-model="form.ztlJtbh" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数据类型" label-width="110px" prop="icName">
            <el-input v-model="form.icName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料代码" label-width="110px" prop="moeWldm">
            <el-input v-model="form.moeWldm" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="品名规格" label-width="110px" prop="itmPmgg">
            <el-input v-model="form.itmPmgg" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始时间" label-width="110px" prop="ztlKssj">
            <el-input v-model="form.ztlKssj" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间" label-width="110px" prop="ztlJssj">
            <el-input v-model="form.ztlJssj" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="操作人员" label-width="110px" prop="ztlWkno">
            <el-input v-model="form.ztlWkno" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="总用时" label-width="110px" prop="ztlGs">
            <el-input v-model="form.ztlGs" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="原因类别" label-width="110px" prop="ztlLbdm">
            <el-select
              v-model="form.ztlLbdm"
              style="width: 270px"
              filterable
              placeholder="请选择原因类别"
              @change="getZtlYyms"
              clearable
            >
              <el-option
                v-for="item in reasonCategoryOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="原因描述" label-width="110px" prop="ztlYyms">
            <el-select
              v-model="form.ztlYyms"
              style="width: 270px"
              filterable
              placeholder="请选择原因类别"
              :disabled="disabled"
            >
              <el-option
                v-for="item in reasonDescribeOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="{ value: item.dictValue, label: item.dictLabel }"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <!--    查询对话框-->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="420px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="98px"
      >
        <el-col :span="24">
          <el-form-item label="机台编号" label-width="100px" prop="moeJtbh">
            <el-input
              style="width: 240px"
              v-model="queryParams.ztlJtbh"
              placeholder="请输入机台编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="日期">
            <el-date-picker
              v-model="daterangeZtl"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24" style="text-align: center">
          <el-radio-group v-model="queryParams.ztlZldm">
            <el-radio
              v-for="dict in reasonShopCategoryOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >
              {{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="openSearch = false">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { exportTable } from "@/utils/ruoyi";
import {
  listDeviceAlter,
  updateDeviceAlter,
} from "@/api/production/deviceAlter";
import {
  editStopReason,
  exportStopReason,
  listStopReason,
} from "@/api/production/stopReason";
import { getReasonDescribleByCategory } from "@/api/system/dict/data";
import { exportCjmOpen } from "@/api/output/cjmOpen";

export default {
  name: "StopReason",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      tableH: document.body.clientHeight - 230,
      show: true,
      openSearch: false,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购属性表格数据
      stopReasonList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      nowData: null,
      disabled: true,
      // 原因类别
      reasonCategoryOptions: [],
      // 原因描述
      reasonDescribeOptions: [],
      // 停机类别
      reasonShopCategoryOptions: [
        { dictLabel: "排程异常", dictValue: "C" },
        { dictLabel: "异常停机", dictValue: "D" },
        { dictLabel: "全部", dictValue: "" },
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 40,
        ztlJtbh: null,
        ztlZldm: "",
      },
      // 建立日期时间范围
      daterangeZtl: [
        this.$moment().startOf("month").toDate(),
        this.$moment().add(100, "year").endOf("month").toDate(),
      ],
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ztlLbdm: [
          { required: true, message: "原因类别不能为空", trigger: "blur" },
        ],
        ztlYyms: [
          { required: true, message: "原因描述不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getDicts("reason_category").then((response) => {
      this.reasonCategoryOptions = response.data;
    });
    this.loading = false;
    this.getList();
  },
  methods: {
    /** 查询采购属性列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeZtl && "" != this.daterangeZtl) {
        this.queryParams.params["beginZtlRq"] = this.moment(
          this.daterangeZtl[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endZtlRq"] = this.moment(
          this.daterangeZtl[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      listStopReason(this.queryParams).then((response) => {
        this.stopReasonList = response.rows;
        this.total = response.total;
        this.loading = false;
        if (this.stopReasonList.length > 0) {
          this.nowData = this.stopReasonList[0];
          this.show = false;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ztlId: null,
        ztlJtbh: null,
        icName: null,
        moeWldm: null,
        itmPmgg: null,
        ztlKssj: null,
        ztlJssj: null,
        ztlWkno: null,
        ztlGs: null,
        ztlLbdm: null,
        ztlYyms: null,
      };
      this.resetForm("form");
    },

    clickRow(row) {
      if (row) {
        this.nowData = row;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleSearch() {
      this.reset();
      this.title = "查询";
      this.openSearch = true;
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form.ztlId = this.nowData.ztlId;
      this.form.ztlJtbh = this.nowData.ztlJtbh;
      this.form.icName = this.nowData.icName;
      this.form.moeWldm = this.nowData.moeWldm;
      this.form.itmPmgg = this.nowData.itmPmgg;
      this.form.ztlKssj = this.nowData.ztlKssj;
      this.form.ztlJssj = this.nowData.ztlJssj;
      this.form.ztlWkno = this.nowData.ztlWkno;
      this.form.ztlGs = this.nowData.ztlGs;
      this.form.ztlLbdm = this.nowData.ztlLbdm;
      this.form.ztlYyms = this.nowData.ztlYyms;
      this.open = true;
      this.title = "分析";
    },

    exportExcel(id, title) {
      // exportTable(id, title)

      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有异常停机原因维护数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportStopReason(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },

    getZtlYyms(data) {
      if (data) {
        this.form.ztlYyms = null;
        getReasonDescribleByCategory(data).then((response) => {
          this.reasonDescribeOptions = response.data;
          this.disabled = false;
        });
      } else {
        this.disabled = true;
      }
    },

    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let s = this.form.ztlYyms;

          this.form.ztlYydm = s.value;
          this.form.ztlYyms = s.label;

          editStopReason(this.form).then((res) => {
            this.getList();
          });
        }
      });
    },
  },
};
</script>
